package com.fumin.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import javax.annotation.Resource;
/**
* @Description:    封装redis使用的类
* @Author:         xiayuxuanmin
* @CreateDate:     2019/11/22 9:56
* @Version:        1.0
*/
//@Service("redisPool")
@Slf4j
public class RedisPool {

    //获取redis.xml中定义的被spring管理的单利对象
    @Resource(name = "shardedJedisPool")
    private ShardedJedisPool shardedJedisPool;

    /**
     * 获取redis实例
     * @return
     */
    public ShardedJedis instance() {
        return shardedJedisPool.getResource();
    }

    /**
     * 安全关闭redis
     * @param shardedJedis
     */
    public void safeClose(ShardedJedis shardedJedis) {
        try {
           if (shardedJedis != null) {
               shardedJedis.close();
           }
        } catch (Exception e) {
            log.error("return redis resource exception", e);
        }
    }
}
